local filename = "../webservice/matamatajogos"
local imprime = false

function redraw()
    file, err = io.open(filename, "r")
    if (err == nil) then
        canvas:attrFont('vera',25,'bold')
        local i = 0
        for line in file:lines() do
            print ("****" .. line)
            time1, value1, value2, time2, pen1, pen2 = string.match(line, "^([a-z-]*)[ ]+([0-9]*)[ ]+([0-9]*)[ ]+([a-z-]*)[ ]*([0-9]*)[ ]*([0-9]*)$")
            matamataTela = string.match(line, "^([a-zA-Z12]*)$")
            if (matamataTela ~= nil and matamataTela == 'mataMata2') then imprime = true elseif (matamataTela ~= nil and matamataTela ~= tela) then imprime = false end
            if (imprime and time1 ~= nil) then
                local img1 = canvas:new(("../media/bandeiras-legendas/" .. time1 .. ".png"))
                local img2 = canvas:new(("../media/bandeiras-legendas/" .. time2 .. ".png"))
                if i < 4 then
                    canvas:compose(708, (63 + i*103), img1)
                    canvas:compose(708, (63 + 49 + i*105), img2)
                    canvas:drawText(679, (65 + i*103), value1)
                    canvas:drawText(679, (65 + 49 + i*105), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(641, (65 + i*103), '*[' .. pen1 .. ']')
                        canvas:drawText(641, (65 + 49 + i*105), '*[' .. pen2 .. ']')
                    end
                elseif i > 3 and i < 6 then
                    canvas:compose(498, (90 + (i - 4)*206), img1)
                    canvas:compose(498, (90 + 102 + (i - 4)*207), img2)
                    canvas:drawText(463, (90 + (i - 4)*204), value1)
                    canvas:drawText(463, (90 + 102 + (i - 4)*208), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(425, (90 + (i - 4)*204), '*[' .. pen1 .. ']')
                        canvas:drawText(425, (90 + 102 + (i - 4)*207), '*[' .. pen2 .. ']')
                    end
                elseif i < 7 then
                    canvas:compose(293, 136, img1)
                    canvas:compose(293, 351, img2)
                    canvas:drawText(256, 137, value1)
                    canvas:drawText(256, 351, value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(218, 137, '*[' .. pen1 .. ']')
                        canvas:drawText(218, 351, '*[' .. pen2 .. ']')
                    end
                else
                    canvas:compose(81, 242, img1)
                    canvas:drawText(39, 243, value1)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(1, 243, '*[' .. pen1 .. ']')
                    end
                end
                i = i + 1
                canvas:flush()
            end
        end
        file:close()
    else 
        print (err)
    end
end

redraw()